package com.hjjt.cameralib.fragment

import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.hjjt.baselib.util.logError
import com.hjjt.cameralib.CameraHelperImpl
import com.hjjt.cameralib.CameraInstance
import com.hjjt.cameralib.cameraengin.CameraEngine
import com.hjjt.cameralib.cameraengin.CameraFactory
import com.hjjt.cameralib.const.CameraConst
import com.hjjt.cameralib.listener.LiveCheckListener
import com.hjjt.cameralib.opengl.GLFrameSurface
import com.hjjt.cameralib.opengl.RoundGLSurfaceView
import com.hjjt.cameraplugin.CameraEngineConfig
import com.hjjt.cameraplugin.ImageData
import com.hjjt.logger.logger
import kotlinx.coroutines.*
import java.lang.ref.WeakReference
import java.util.*
import kotlin.concurrent.timerTask


/**
 *
 * @ProjectName:    CameraLib
 * @Package:        com.hjjt.cameralib.fragment
 * @ClassName:      GLSurfaceFragment
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/6/2 15:08
 */
class GLSurfaceFragment private constructor() : Fragment(), CoroutineScope by MainScope() {
    //    var openCameraTime = 0L
    var camInstance = CameraInstance.instance


    lateinit var surfaceView: GLFrameSurface

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        surfaceView = RoundGLSurfaceView(requireContext()).apply {
            setBackgroundColor(Color.parseColor("#00FFFFFF"))
        }
        return surfaceView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        openCameraAndPreview(view as GLFrameSurface)
    }

    private fun openCameraAndPreview(surfaceView: GLFrameSurface) {
        logError("开始启动摄像头")
        val angle = arguments?.getSerializable("angle") as CameraEngineConfig.Angle
        val type = arguments?.getSerializable("type") as CameraEngineConfig.CameraEngineType
        launch(Dispatchers.IO) {
            withContext(Dispatchers.IO) {
                camInstance.init(angle, type)
                camInstance.initSurfaceView(surfaceView)
            }
            launch(Dispatchers.Main) {
                camInstance.openOrCloseStream(true)
                camInstance.bindToLifecycle(this@GLSurfaceFragment)
            }
        }

//        arguments?.let {
//
//            camInstance.run {
//
//
//
//
//                setPreviewCallBack {
//                    if (!showPreview) {
////                            CameraHelperImpl.cameraOpenSuccess?.invoke()
////                            startDetect()
//                        showPreview = true
////                            Handler(Looper.getMainLooper()).postDelayed({
////                                GlobalScope.launch {
////                                    withContext(Dispatchers.IO){
////                                        camInstance.openOrCloseStream(false)
////                                        camInstance.stopDetect()
////                                    }
//////                                    async(Dispatchers.Main) {
//////                                        CameraHelperImpl.cameraDetectComplete?.invoke()
//////                                    }
////                                }
////                            }, 2000)
//                    }
//                }
//            }
//        }
//        GlobalScope.launch(Dispatchers.Main) {
//
//            launch(Dispatchers.Main) {
//                camInstance.bindToLifecycle(this@GLSurfaceFragment)
//            }
//        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        camInstance.release()
        surfaceView.setmRenderer(null)
        camInstance.initSurfaceView(null)
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onResume() {
        super.onResume()
//        openCameraAndPreview()
    }


    companion object {
        fun newInstance(
            cameraEngineType: CameraEngineConfig.CameraEngineType,
            angle: CameraEngineConfig.Angle
        ): GLSurfaceFragment {
            val fragment = GLSurfaceFragment()
            val args = Bundle()
            args.putSerializable("angle", angle)
            args.putSerializable("type", cameraEngineType)
            fragment.arguments = args
            return fragment
        }
    }


}